<?php
class Core_Driver_Sessiondatabase extends Core_Library_Session {

	// database connection
	private $db;

	public function __construct(){
		try {
			if(class_exists('Core_Driver_MysqlImproved')){
				$this->db = new Core_Driver_MysqlImproved();
			}
			else {
				throw new Exception("Nie można załadować klasy do obsługi bazy danych!");
			}
		}
		catch(Exception $e){
			return $e->getMessage();
		}
	}


	public function getUserInfo($username = null){
		try {
			$this->db->connect();
			$username = $this->db->escape($username);
			$this->db->prepare(
				"
				SELECT
				*
				FROM
				`users`
				WHERE
				`username` = '$username'
				LIMIT
				1
				;
				"
			);
			$this->db->query();
			$userInfo = $this->db->fetch('array');
			if($userInfo != null){
				return $userInfo;
			}
			else {
				throw new Exception("Nie udało się znaleźć wybranego użytkownika!");
				return null;
			}
		}
		catch(Exception $e){
			return $e->getMessage();
		}
	}

	public function checkUser($userName = null, $userPassword = null){
		$this->db->connect();
		$userName = $this->db->escape($userName);
		$userPassword = $this->db->escape($userPassword);
		$this->db->prepare(
				"
				SELECT
				*
				FROM
				`users`
				WHERE
				`username` = '$userName'
				AND
				`password` = '$userPassword'
				LIMIT
				1
				;
				"
		);
		return $this->db->query();
	}
}